<template>
  <div class="login-center">
    <h1>登录</h1>
    <div class="login-form">
      <label for="username">用户名</label>
      <input v-model="username" id="username" type="text" placeholder="用户名">
      <label for="password">密码</label>
      <input v-model="password" id="password" type="password" placeholder="密码">
    </div>
    <button @click="login">登录</button>
    <p v-if="loginMessage" class="login-message">{{ loginMessage }}</p>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      username: '',
      password: '',
      loginMessage: ''
    };
  },
  methods: {
    async login() {
      console.log("尝试登录", this.username, this.password);
      try {
        const response = await axios.post('http://localhost:8080/admin-users/login', {
          username: this.username,
          password: this.password // 修正拼写错误
        });
        console.log('登录响应:', response.data);
        if (response.data.message === '登录成功') {
          this.$router.push('/admin');
        } else {
          this.loginMessage = response.data.message || '登录失败，请检查用户名和密码。';
        }
      } catch (error) {
        console.error('登录错误:', error);
        this.loginMessage = '登录失败，请检查用户名和密码。';
      }
    }
  }
}
</script>

<style scoped>
.login-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-color: #f5f5f5;
}

h1 {
  margin-bottom: 20px;
}

.login-form {
  display: flex;
  flex-direction: column;
  margin-bottom: 20px;
}

label {
  margin-bottom: 5px;
}

input {
  width: 200px;
  padding: 10px;
  margin-bottom: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

button {
  padding: 10px 20px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #0056b3;
}

.login-message {
  margin-top: 20px;
  color: red;
}
</style>
